﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace TradeForm
{
    public partial class FrmCode : Form
    {
        public string ReturnName = string.Empty;
        private string type = string.Empty;

        public FrmCode()
        {
            InitializeComponent();
        }

        public FrmCode(string type)
        {
            InitializeComponent();
            this.type = type;
        }

        //private void txtSrchName_TextChanged(object sender, EventArgs e)
        //{

        //    foreach (DataGridViewRow row in this.dataGridView1.Rows)
        //    {
        //        if (row.Cells[2].Value != null)
        //        {
        //            if (row.Cells[2].Value.ToString().ToLower().IndexOf(this.txtSrchName.Text.ToLower()) == 0)
        //            {
        //                // row.Selected = true;
        //                this.dataGridView1.CurrentCell = row.Cells[0];

        //                return;
        //            }
        //        }

        //    }
        //}

        private void FrmCode_Load(object sender, EventArgs e)
        {
            //DataDBDataContext dbcntxt = new DataDBDataContext
            //       (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);
            //var ports = from port in dbcntxt.Port
            //            where port.EName.Contains(this.txtSrchName.Text)
            //            select port;
            //this.dataGridView1.DataSource = ports;
            Findit();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Rows.Count == 1)
            {
                ReturnName = this.dataGridView1.Rows[0].Cells["ColEName"].Value.ToString().ToUpper ();
            }
            else if (this.dataGridView1.SelectedRows.Count > 0)
            {
                ReturnName = this.dataGridView1.SelectedRows[0].Cells["ColEName"].Value.ToString().ToUpper();
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            Findit();
            //DataDBDataContext dbcntxt = new DataDBDataContext
            //       (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);
            //if (string.IsNullOrEmpty ( this.type))
            //{
            //    var ports = from port in dbcntxt.Port
            //                where port.PortCode.Contains(this.txtCode.Text) || port.EName.Contains(this.txtEName)
            //                select port;
            //    this.dataGridView1.DataSource = ports;
            //}


        }

        private void Findit()
        {
            DataTable dt = new DataTable();
            DataColumn col = new DataColumn("Code");
            dt.Columns.Add(col);
            col = new DataColumn("CName");
            dt.Columns.Add(col);
            col = new DataColumn("EName");
            dt.Columns.Add(col);
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    if (string.IsNullOrEmpty(this.type) || this.type == "port")
                    {
                        cmd.CommandText = "select PortCode as Code ,CName as CName,EName as EName from port where 1=1 "
                            + (string.IsNullOrEmpty(this.txtCode.Text) ? "" : " and PortCode like '%" + this.txtCode.Text + "%'")
                            + (string.IsNullOrEmpty(this.txtCName.Text) ? "" : " and CName like '%" + this.txtCName.Text + "%'")
                             + (string.IsNullOrEmpty(this.txtEName.Text) ? "" : " and EName like '%" + this.txtEName.Text + "%'");
                    }
                    else if (this.type == "country")
                    {
                        cmd.CommandText = "select  CountryCode as Code ,CName as CName,EName as EName from Country where 1=1 "
                            + (string.IsNullOrEmpty(this.txtCode.Text) ? "" : " and CountryCode like '%" + this.txtCode.Text + "%'")
                            + (string.IsNullOrEmpty(this.txtCName.Text) ? "" : " and CName like '%" + this.txtCName.Text + "%'")
                             + (string.IsNullOrEmpty(this.txtEName.Text) ? "" : " and EName like '%" + this.txtEName.Text + "%'");
                    }
                    if (string.IsNullOrEmpty(cmd.CommandText))
                    {
                        return;
                    }
                    con.Open();
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    ad.Fill(dt);
                    this.dataGridView1.DataSource = dt;
                }
            }
        }
    }
}
